package com.onlinevideo.servlet;

import cn.hutool.json.JSONUtil;
import com.onlinevideo.data.ResultBean;
import com.onlinevideo.entity.Comment;
import com.onlinevideo.entity.Message;
import com.onlinevideo.entity.User;
import com.onlinevideo.entity.Video;
import com.onlinevideo.service.MessageService;
import com.onlinevideo.service.impl.CommentServiceImpl;
import com.onlinevideo.service.impl.MessageServiceImpl;
import com.onlinevideo.service.impl.VideoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class CommentServlet extends BaseServlet {
    CommentServiceImpl commentService = new CommentServiceImpl();
    MessageService messageService = new MessageServiceImpl();
    VideoServiceImpl videoService = new VideoServiceImpl();
    PrintWriter writer;

    //添加评论
    protected void insertComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute("user");
        String fidStr = req.getParameter("fid");
        String vidStr = req.getParameter("vid");
        String content = req.getParameter("commentContent");
        Long fid = null;
        Long vid = null;
        if (vidStr == null && "-1".equals(fidStr)) {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("评论失败")));
            return;
        }
        if (vidStr != null) {
            vid = Long.valueOf(vidStr);
            Video video = videoService.queryById(vid);
            Long userId = videoService.queryById(vid).getUserId();
            Message message = new Message(null, userId, " 有人回复您的评论，评论内容：" + content + ";<a href=" + req.getContextPath() + "../video.do?action=play&videoId=" + video.getId() + ">视频链接</a>", 0, String.valueOf(System.currentTimeMillis()));
            messageService.insert(message);
        }
        if (fidStr != null) {
            fid = Long.valueOf(fidStr);
            Comment comment = commentService.queryByCommonId(fid);
            Long userId = commentService.queryByCommonId(fid).getUserId();
            Video video = videoService.queryById(comment.getVideoId());
            Message message = new Message(null, userId, " 有人回复您的评论，评论内容：" + content + ";<a target=\"_blank\" href=" + req.getContextPath() + "../video.do?action=play&videoId=" + video.getId() + ">视频链接</a>", 0, String.valueOf(System.currentTimeMillis()));
            messageService.insert(message);

        }
        Object[] objects = commentService.addComment(fid, vid, user, content);
        if ((int) (objects[5]) > 0) {
            String test = JSONUtil.toJsonStr(ResultBean.scOperationSuccess("评论成功", objects));
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("评论成功", objects)));
        } else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("评论失败")));
        }
    }

    //删除评论
    protected void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute("user");
        String idStr = req.getParameter("id");
        Long id = null;
        if (idStr != null) {
            id = Long.valueOf(idStr);
        }
        Comment comment = commentService.queryById(id);
        PrintWriter writer = resp.getWriter();
        if (!user.getId().equals(comment.getUserId())) {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("不能删除别人的评论")));
            return;
        }
        int delete = commentService.delete(id);
        if (delete > 0) {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("删除评论成功")));
        } else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("删除评论失败")));
        }
    }

    //评论举报
    protected void reportComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        writer = resp.getWriter();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            user = new User();
            user.setId(1L);
        }
        String commentIdStr = req.getParameter("commentId");
        Long commentId = Long.valueOf(commentIdStr);
        Comment comment = commentService.queryByCommonId(commentId);
        if (comment == null) {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("举报评论失败，评论不存在")));
            return;
        }
        comment.setCommentStatus(0);
        int update = commentService.update(comment);

        writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("举报评论成功")));
        Message message = new Message(null, comment.getUserId(), "您的评论\"" + comment.getCommentContent() + "\"已被举报，正在审核", 0, String.valueOf(System.currentTimeMillis()));
        messageService.insert(message);

    }

    //评论审核
    protected void checkComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        writer = resp.getWriter();
        String commentIdStr = req.getParameter("commentId");
        String delete = req.getParameter("delete");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            user = new User();
            user.setId(1L);
        }
        if (commentIdStr == null) {
            return;
        }
        Long commentId = Long.valueOf(commentIdStr);
        Comment comment = commentService.queryById(commentId);
        if ("true".equals(delete)) {
            commentService.delete(commentId);
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("评论已删除")));
            Message message = new Message(null, comment.getUserId(), "您的评论\"" + comment.getCommentContent() + "\"审核完成，已被删除", 0, String.valueOf(System.currentTimeMillis()));
            messageService.insert(message);
        } else if ("false".equals(delete)) {
            comment.setCommentStatus(1);
            commentService.update(comment);
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("评论已通过")));
            Message message = new Message(null, comment.getUserId(), "您的评论\"" + comment.getCommentContent() + "\"审核完成，已经还原", 0, String.valueOf(System.currentTimeMillis()));
            messageService.insert(message);
        }
    }
}
